package top.yokey.android.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import top.yokey.android.R;
import top.yokey.android.base.BaseActivity;
import top.yokey.android.http.BaseCallBack;
import top.yokey.android.http.HttpHelp;

public class ModifyPasswordActivity extends BaseActivity {

    private AppCompatImageView backImageView;
    private AppCompatEditText oldPassEditText;
    private AppCompatEditText newPassEditText;
    private View oldPassView, newPassView;
    private AppCompatTextView modifyTextView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_modify_password);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {

        backImageView = findViewById(R.id.backImageView);
        oldPassEditText = findViewById(R.id.oldPassEditText);
        newPassEditText = findViewById(R.id.newPassEditText);
        oldPassView = findViewById(R.id.oldPassView);
        newPassView = findViewById(R.id.newPassView);
        modifyTextView = findViewById(R.id.modifyTextView);

    }

    @Override
    public void initEvent() {

        backImageView.setOnClickListener(view -> finish());

        oldPassEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                oldPassView.setBackgroundResource(R.color.primary);
                newPassView.setBackgroundResource(R.color.textHint);
            }
        });

        newPassEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                oldPassView.setBackgroundResource(R.color.textHint);
                newPassView.setBackgroundResource(R.color.primary);
            }
        });

        modifyTextView.setOnClickListener(view -> {
            String oldPass = oldPassEditText.getText().toString();
            String newPass = newPassEditText.getText().toString();
            if (oldPass.isEmpty() || newPass.isEmpty()) {
                Toast.makeText(this, "请输入旧密码或者新密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (newPass.length() < 6) {
                Toast.makeText(this, "新密码的长度不能小于6位", Toast.LENGTH_SHORT).show();
                return;
            }
            modifyTextView.setEnabled(false);
            modifyTextView.setText("正在修改密码...");
            HttpHelp.get().ready("member", "modifyPassword")
                    .add("old_pass", oldPass)
                    .add("new_pass", newPass)
                    .get(new BaseCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            Toast.makeText(ModifyPasswordActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }

                        @Override
                        public void onError(String reason) {
                            Toast.makeText(ModifyPasswordActivity.this, reason, Toast.LENGTH_SHORT).show();
                            modifyTextView.setEnabled(true);
                            modifyTextView.setText("修改密码");
                        }
                    });
        });

    }

}
